package io.reactivex.rxohos2;

import com.polidea.rxohosble2.sample.DeviceAbility;
import com.polidea.rxohosble2.sample.ResourceTable;
import com.polidea.rxohosble2.sample.example1_scanning.ScanAbility;
import com.polidea.rxohosble2.sample.example2_connection.ConnectionExampleAbility;
import com.polidea.rxohosble2.sample.example3_discovery.ServiceDiscoveryExampleAbility;
import com.polidea.rxohosble2.sample.example4_characteristic.CharacteristicOperationExampleAbility;
import io.reactivex.rxohos2.utile.EventHelper;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.ability.delegation.AbilityDelegatorRegistry;
import ohos.aafwk.ability.delegation.IAbilityDelegator;
import ohos.agp.components.*;
import org.junit.Test;

import static org.junit.Assert.*;

public class SampleApplicationTest {

    IAbilityDelegator sAbilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator();
    String BLEName = "ZKTeco";//需要连接的蓝牙名称(需要可读写)
    String address = "18:93:7F:E8:2F:14";//蓝牙地址
    private Ability ability;

    @Test
    public void testOnStart() {
         ability = EventHelper.startAbility(ScanAbility.class);
        goToSleep(5000);
        Button start = (Button) ability.findComponentById(ResourceTable.Id_scan_toggle_btn);
        EventHelper.triggerClickEvent(ability, start);
        goToSleep(8000);
        ListContainer componentById = (ListContainer) ability.findComponentById(ResourceTable.Id_scan_results);
        Text componentById1 = (Text) componentById.getComponentAt(0).findComponentById(ResourceTable.Id_text1);
        assertTrue(!componentById1.getText().equals(""));
    }

    @Test
    public void testOnConnect() {
         ability = getAbility();
        ListContainer listContainer = (ListContainer) ability.findComponentById(ResourceTable.Id_scan_results);
        for (int i = 0; i < 15; i++) {
            Text text = (Text) listContainer.getComponentAt(i).findComponentById(ResourceTable.Id_text1);
            if (text.getText().equals(address + " (" + BLEName + ")")) {
                ConnectionExampleAbility connectionExampleAbility = getConnectionExampleAbility(ability, listContainer, i);
                Text textstate = (Text) connectionExampleAbility.findComponentById(ResourceTable.Id_connection_state);
                assertTrue(!textstate.getText().equals(""));
                return;
            } else {
                continue;
            }
        }
    }

    @Test
    public void testAutoOnConnect() {
        Ability ability = getAbility();
        ListContainer listContainer = (ListContainer) ability.findComponentById(ResourceTable.Id_scan_results);
        for (int i = 0; i < 15; i++) {
            Text text = (Text) listContainer.getComponentAt(i).findComponentById(ResourceTable.Id_text1);
            if (text.getText().equals(address + " (" + BLEName + ")")) {
                ConnectionExampleAbility connectionExampleAbility = getConnectionExampleAbility(ability, listContainer, i);
                Component aSwitch =  connectionExampleAbility.findComponentById(ResourceTable.Id_autoconnect);
                aSwitch.setEnabled(true);
                EventHelper.triggerClickEvent(connectionExampleAbility,aSwitch);
                goToSleep(5000);
                Button componentById = (Button) connectionExampleAbility.findComponentById(ResourceTable.Id_connect_toggle);
                EventHelper.triggerClickEvent(connectionExampleAbility,componentById);
                goToSleep(2000);
                Text textstate = (Text) connectionExampleAbility.findComponentById(ResourceTable.Id_connection_state);
                goToSleep(2000);
                assertTrue(!textstate.getText().equals(""));
                return;
            } else {
                continue;
            }
        }
    }

    private Ability getAbility() {
        Ability ability = EventHelper.startAbility(ScanAbility.class);
        goToSleep(5000);
        Button start = (Button) ability.findComponentById(ResourceTable.Id_scan_toggle_btn);
        EventHelper.triggerClickEvent(ability, start);
        goToSleep(5000);
        return ability;
    }

    @Test
    public void testSetMtu(){
        Ability ability = getAbility();
        ListContainer listContainer = (ListContainer) ability.findComponentById(ResourceTable.Id_scan_results);
        for (int i = 0; i < 15; i++) {
            Text text = (Text) listContainer.getComponentAt(i).findComponentById(ResourceTable.Id_text1);
            if (text.getText().equals(address + " (" + BLEName + ")")) {
                ConnectionExampleAbility connectionExampleAbility = getConnectionExampleAbility(ability, listContainer, i);
                goToSleep(3000);
                TextField textField = (TextField) connectionExampleAbility.findComponentById(ResourceTable.Id_newMtu);
                textField.setText(55);
                goToSleep(2000);
                Button setMtu = (Button) connectionExampleAbility.findComponentById(ResourceTable.Id_set_mtu);
                EventHelper.triggerClickEvent(connectionExampleAbility,setMtu);
                goToSleep(6000);
                assertTrue(connectionExampleAbility.isMtuFlage());
                return;
            } else {
                continue;
            }
        }
    }

    @Test
    public void testRead(){
        ability = getAbility();
        ListContainer listContainer = (ListContainer) ability.findComponentById(ResourceTable.Id_scan_results);
        for (int i = 0; i < 15; i++) {
            Text text = (Text) listContainer.getComponentAt(i).findComponentById(ResourceTable.Id_text1);
            if (text.getText().equals(address + " (" + BLEName + ")")) {
                Component componentById2 = listContainer.getComponentAt(i - listContainer.getItemPosByVisibleIndex(0));
                EventHelper.triggerClickEvent(ability, componentById2);
                goToSleep(3000);
                CharacteristicOperationExampleAbility readWriteAbility = getReadWriteAbility(listContainer);
                Button read = (Button) readWriteAbility.findComponentById(ResourceTable.Id_read);
                EventHelper.triggerClickEvent(readWriteAbility,read);
                goToSleep(3000);
                Text readVaule = (Text) readWriteAbility.findComponentById(ResourceTable.Id_read_hex_output);
                assertTrue(!readVaule.getText().equals(""));
                return;
            } else {
                continue;
            }
        }
        }
    @Test
    public void testWrite(){
        ability = getAbility();
        ListContainer listContainer = (ListContainer) ability.findComponentById(ResourceTable.Id_scan_results);
        for (int i = 0; i < 15; i++) {
            Text text = (Text) listContainer.getComponentAt(i).findComponentById(ResourceTable.Id_text1);
            if (text.getText().equals(address + " (" + BLEName + ")")) {
                Component componentById2 = listContainer.getComponentAt(i - listContainer.getItemPosByVisibleIndex(0));
                EventHelper.triggerClickEvent(ability, componentById2);
                goToSleep(3000);
                CharacteristicOperationExampleAbility readWriteAbility = getReadWriteAbility(listContainer);
                goToSleep(3000);
                Button button = (Button) readWriteAbility.findComponentById(ResourceTable.Id_connect);
                if (button.getText().equals("Disconnect")){
                TextField textField = (TextField) readWriteAbility.findComponentById(ResourceTable.Id_write_input);
                textField.setText(55);
                goToSleep(3000);
                Button write = (Button) readWriteAbility.findComponentById(ResourceTable.Id_write);
                EventHelper.triggerClickEvent(readWriteAbility,write);
                goToSleep(3000);
                assertTrue(readWriteAbility.isWriteFlage());
                return;
                }else if (button.getText().equals("Connecting")){
                    goToSleep(5000);
                }else {
                    assertTrue(false);
                    return;
                }
                return;
            } else {
                continue;
            }
        }
    }



//    @org.jetbrains.annotations.NotNull
    private CharacteristicOperationExampleAbility getReadWriteAbility(ListContainer listContainer) {
        DeviceAbility currentTopAbility = (DeviceAbility) sAbilityDelegator.getCurrentTopAbility();
        Button serviece = (Button) currentTopAbility.findComponentById(ResourceTable.Id_discovery);
        EventHelper.triggerClickEvent(currentTopAbility, serviece);
        goToSleep(5000);
        ServiceDiscoveryExampleAbility serviceDiscoveryExampleAbility = (ServiceDiscoveryExampleAbility) sAbilityDelegator.getCurrentTopAbility();
        Button componentById = (Button) serviceDiscoveryExampleAbility.findComponentById(ResourceTable.Id_connect);
        EventHelper.triggerClickEvent(serviceDiscoveryExampleAbility,componentById);
        goToSleep(5000);
        ListContainer listContainer1 = (ListContainer) serviceDiscoveryExampleAbility.findComponentById(ResourceTable.Id_scan_results);
        Component componentAt = listContainer1.getComponentAt(2 - listContainer.getItemPosByVisibleIndex(0));//根据需要选择可以读写的item
        EventHelper.triggerClickEvent(serviceDiscoveryExampleAbility,componentAt);
        goToSleep(3000);
        CharacteristicOperationExampleAbility readWriteAbility = (CharacteristicOperationExampleAbility) sAbilityDelegator.getCurrentTopAbility();
        Button connecting = (Button) readWriteAbility.findComponentById(ResourceTable.Id_connect);
        EventHelper.triggerClickEvent(readWriteAbility,connecting);
        goToSleep(6000);
        return readWriteAbility;
    }


    private ConnectionExampleAbility getConnectionExampleAbility(Ability ability, ListContainer listContainer, int i) {
        Component componentById2 = listContainer.getComponentAt(i - listContainer.getItemPosByVisibleIndex(0));
        EventHelper.triggerClickEvent(ability, componentById2);
        goToSleep(3000);
        DeviceAbility currentTopAbility = (DeviceAbility) sAbilityDelegator.getCurrentTopAbility();
        Button conect = (Button) currentTopAbility.findComponentById(ResourceTable.Id_connect);
        EventHelper.triggerClickEvent(currentTopAbility, conect);
        goToSleep(5000);
        ConnectionExampleAbility connectionExampleAbility = (ConnectionExampleAbility)sAbilityDelegator.getCurrentTopAbility();
        Button componentById = (Button) connectionExampleAbility.findComponentById(ResourceTable.Id_connect_toggle);
        EventHelper.triggerClickEvent(connectionExampleAbility,componentById);
        goToSleep(5000);
        return connectionExampleAbility;
    }

    private void goToSleep(long s) {
        try {
            Thread.sleep(s);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}